package com.lzl.artisan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzl.artisan.model.Passage;
import com.lzl.artisan.model.Result;
import com.lzl.artisan.model.dto.PassageQueryDto;
import com.lzl.artisan.service.PassageService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/passage")
public class PassageController extends BaseController{

    @Autowired
    private PassageService passageService;

    @ApiOperation("根据分类编号、文章名称多条件查询所有文章")
    @GetMapping("list")
    public Result listByCategoryId(PassageQueryDto passageQueryDto){
        QueryWrapper<Passage> wrapper = new QueryWrapper<>();
        wrapper.eq(!StringUtils.isEmpty(passageQueryDto.getCategoryId()),"category_id",passageQueryDto.getCategoryId());
        wrapper.like(!StringUtils.isEmpty(passageQueryDto.getTitle()),"title",passageQueryDto.getTitle());
        Page<Passage> page = passageService.page(getPage(), wrapper);
        return Result.ok(page);
    }

    @ApiOperation("根据文章编号查询文章")
    @GetMapping("info/{id}")
    public Result info(@PathVariable Long id){
        Passage passage = passageService.getById(id);
        return Result.ok(passage);
    }

}
